package telcoblocks.gen;
import java.io.File;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.dialogs.WizardDataTransferPage;
import org.eclipse.ui.dialogs.WizardExportResourcesPage;

public class ExportConfigurationWizardPage extends WizardExportResourcesPage {
	
	private Button destinationBrowseButton;
	//private Button headerCheckbox;
	private Text destinationPath;

	/**
	 * ExportConfigurationWizard Page Constructor
	 * 
	 * @param pageName
	 * @param selection
	 */
	protected ExportConfigurationWizardPage(String pageName, IStructuredSelection selection) {
		super("FileSystemExportPage", null); //$NON-NLS-1$
		setTitle("Exportar configuración");
		setDescription("Exportar diagrama a ficheros de configuración");
	}
	
	
	@Override
	protected final void createDestinationGroup(Composite parent) {
		Font font = parent.getFont();
		Composite destinationSelectionGroup = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		destinationSelectionGroup.setLayout(layout);
		destinationSelectionGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
		destinationSelectionGroup.setFont(font);

		Label destinationLabel = new Label(destinationSelectionGroup, SWT.NONE);
		destinationLabel.setText("Destino");
		destinationLabel.setFont(font);

		// destination name entry field
		destinationPath = new Text(destinationSelectionGroup, SWT.SINGLE | SWT.BORDER);
		destinationPath.addListener(SWT.Modify, this);
		destinationPath.addListener(SWT.Selection, this);
		GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL);
		data.widthHint = SIZING_TEXT_FIELD_WIDTH;
		destinationPath.setLayoutData(data);
		destinationPath.setFont(font);

		// destination browse button
		destinationBrowseButton = new Button(destinationSelectionGroup, SWT.PUSH);
		destinationBrowseButton.setText("Browse");
		destinationBrowseButton.addListener(SWT.Selection, this);
		setButtonLayoutData(destinationBrowseButton);

		new Label(parent, SWT.NONE); // vertical spacer;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.dialogs.WizardDataTransferPage#createOptionsGroupButtons(org.eclipse.swt.widgets.Group)
	 */
	protected final void createOptionsGroupButtons(Group optionsGroup) {
		optionsGroup.dispose();
		/*
		Font font = optionsGroup.getFont();
		createHeader(optionsGroup, font);
		*/
	}

	/**
	 * Checkbox creation
	 * 
	 * @param optionsGroup options
	 * @param font font
	 */
	/*
	protected final void createHeader(Group optionsGroup, Font font) {
		headerCheckbox = new Button(optionsGroup, SWT.CHECK | SWT.LEFT);
		headerCheckbox.setText("Generate Asterisk Configuration Files (Sip.conf)");
		headerCheckbox.setFont(font);

	}
	*/

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
	 */
	public final void handleEvent(Event e) {
		Widget source = e.widget;
		if (source == destinationBrowseButton) {
			handleDestinationBrowseButtonPressed();
		}
		updatePageCompletion();
	}

	/**
	 * Action for DestinationBrowseButton pressed
	 */
	protected final void handleDestinationBrowseButtonPressed() {
		DirectoryDialog dialog = new DirectoryDialog(getContainer().getShell(), SWT.SAVE);
		dialog.setMessage("Choose a directory where to export your files (or create it if necessary");
		dialog.setText("Choose an export directory");
		dialog.setFilterPath(getSelectedDirectory());
		String selectedDirectoryName = dialog.open();

		if (selectedDirectoryName != null) {
			setErrorMessage(null);
			setDestinationField(selectedDirectoryName);
		}
	}

	/**
	 * Gets a list with the selected Resources
	 * @return Une liste de ressources
	 */
	@SuppressWarnings("unchecked") //$NON-NLS-1$
	public final List<IResource> getSelectedResources() {
		List<IResource> selectedResources = getWhiteCheckedResources();
		return selectedResources;
	}

	/**
	 * Gets the destinationField value
	 * 
	 * @return String that represents the path to the destination chosen
	 */
	protected final String getSelectedDirectory() {
		return destinationPath.getText().trim();
	}

	/**
	 * Sets the value of destinationField
	 * 
	 * @param value name of the destination
	 */
	protected final void setDestinationField(String value) {
		destinationPath.setText(value);
		notify();
	}

	/**
	 * Checks if the destination is valid
	 * 
	 * @param destination destination to verify
	 * @return true if destination directory is valid, false if not
	 */
	protected final boolean isDestinationValid(File destination) {
		if (destination.exists()){
			if(destination.isDirectory()){
				return true;
			}
			else{
				displayErrorDialog("La selección no es un directorio!!!");
				destinationPath.setFocus();
				return false;			
			}
		}
		else if (queryYesNoQuestion("El directorio de destino no existe. ¿Desea crearlo?") & destination.mkdirs())
			return true;
		else if (!destination.mkdirs()){
			displayErrorDialog("No ha sido posible crear el directorio");
			destinationPath.setFocus();
			return false;
		}
		return false;
	}
}
